package reflaction;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @author cyt
 * @create 2020-04-20 10:06
 */
public class Test11 {

    public Map<String, String> test1() {
        System.out.println("test1");
        return null;
    }

    public void test2(Map<String, String> map, List<Integer> list) {
        System.out.println("test2");
    }

    /**
     * 通过反射来获得泛型的信息
     *
     * @param args
     */
    public static void main(String[] args) throws NoSuchMethodException {
        Method method = Test11.class.getDeclaredMethod("test2", Map.class, List.class);
        Type[] parameterTypes = method.getGenericParameterTypes();
        for (Type type : parameterTypes) {
            System.out.println(type);
            if (type instanceof ParameterizedType) {

                Type[] types = ((ParameterizedType) type).getActualTypeArguments();
                for (Type type1 : types) {

                    System.out.println(type1);
                }
            }
        }

        method = Test11.class.getDeclaredMethod("test1", (Class<?>) null);
        Type returnType = method.getGenericReturnType();
        System.out.println(returnType);
        if (returnType instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType) returnType).getActualTypeArguments();
            for (Type type1 : types) {
                System.out.println(type1);
            }
        }

    }
}
